<?php

class CommentController extends Controller {
	public function listAction() {
		$requestData = [];
		$requestData['page'] = intval(Request::get('page', 1));
		$requestData['pageSize'] = intval(Request::get('pageSize', 15));
		$requestData['typeId'] = intval(Request::get('typeId'));
		$requestData['type'] = intval(Request::get('type', 1));
		$where = $value = [];
		$order = 'c.comment_id desc';
		$where[] = 'c.status=1';
		$where[] = 'c.type=?';
		$value[] = $requestData['type'];
		if ($requestData['typeId']) {
			$where[] = 'c.type_id=?';
			$value[] = $requestData['typeId'];
		}
		$where[] = 'c.parent_id =?';
		$value[] = 0;

		Response::ajax(200, 'success',
			self::model('comment')->getListWithUser($where, $value, $order, $requestData['page'], $requestData['pageSize']));
	}

	public function infoAction() {
		$comment_id = intval(Request::get('commentId'));
		Response::ajax(200, 'success',
			self::model('comment')->getListWithUser1($comment_id));
	}

	public function isUpAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$type = intval(Request::getParams('type'));
		$typeId = intval(Request::getParams('typeId'));
		$commentIds = Request::getParams('commentId');
		Response::ajax(200, 'success',
			self::model('comment_up')->getUpIds($userInfo['uid'], $type, $commentIds));
	}

	public function upAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$requestData = [];
		$requestData['type'] = intval(Request::getParams('type', 1));
		$requestData['typeId'] = intval(Request::getParams('typeId'));
		$requestData['commentId'] = intval(Request::getParams('commentId'));
		if (!$requestData['type'] || !$requestData['commentId']) {
			Response::ajax(NORMAL_ERROR, '参数有误,点赞失败');
		}
		try {
			if ($requestData['type'] == commentModel::DYNAMIC) {
				if (self::model('comment_up')->insert([
					'uid' => $userInfo['uid'],
					'comment_id' => $requestData['commentId'],
					'type' => $requestData['type'],
				])) {
					self::model('comment')->execute("update wf_comment set up_total=up_total+1 where comment_id={$requestData['commentId']}");
					Response::ajax(200, '点赞成功');
				}
			} else {
				Response::ajax(NORMAL_ERROR, '参数有误,类型选择有误');
			}
		} catch (Exception $exc) {
			Response::ajax(NORMAL_ERROR, '您已点赞');
			Log::write('评论点赞失败:' . $exc->getTraceAsString(), 'WARN');
		}
	}

	public function removeupAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$requestData = [];
		$requestData['type'] = intval(Request::getParams('type', 1));
		$requestData['typeId'] = intval(Request::getParams('typeId'));
		$requestData['commentId'] = intval(Request::getParams('commentId'));
		if (!$requestData['type'] || !$requestData['commentId']) {
			Response::ajax(NORMAL_ERROR, '参数有误,取消点赞失败');
		}
		try {
			if ($requestData['type'] == commentModel::DYNAMIC) {
				if (self::model('comment_up')->delete([
					'uid' => $userInfo['uid'],
					'comment_id' => $requestData['commentId'],
					'type' => $requestData['type'],
				])) {
					self::model('comment')->execute("update wf_comment set up_total=up_total-1 where comment_id={$requestData['commentId']}");
					Response::ajax(200, '取消点赞成功');
				}
			} else {
				Response::ajax(NORMAL_ERROR, '参数有误,类型选择有误');
			}
		} catch (Exception $exc) {
			Response::ajax(NORMAL_ERROR, '您已取消点赞');
			Log::write('取消评论点赞失败:' . $exc->getTraceAsString(), 'WARN');
		}
	}

	public function addAction() {
		if (!$userInfo = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!$userInfo = self::model('user')->findById($userInfo['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$requestData = [];
		$requestData['content'] = Security::removeXss(Request::getParams('content'));
		$requestData['image_list'] = Security::removeXss(Request::getParams('imageList'));
		$requestData['parent_id'] = intval(Request::getParams('commentId'));
		$requestData['score'] = intval(Request::getParams('score'));
		$requestData['day'] = date('Y-m-d');
		$requestData['add_time'] = time();
		$requestData['update_time'] = time();
		$requestData['type'] = intval(Request::getParams('type', 1));
		$requestData['status'] = 1;
		$requestData['type_id'] = intval(Request::getParams('typeId'));
		$requestData['uid'] = $userInfo['uid'];
		$result = self::model('comment')->validata($requestData);
		if ($result !== true) {
			Response::ajax(NORMAL_ERROR, $result);
		}
		if (!$requestData['image_list']) {
			unset($requestData['image_list']);
		}
		if ($requestData['type'] == commentModel::DYNAMIC) {
			if (!$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $requestData['type_id'])) {
				Response::ajax(NORMAL_ERROR, '参数有误');
			}
			$requestData['title'] = $dynamicInfo['title'];
			if ($requestData['parent_id']) {
				$parentComment = self::model('comment')->getOne('comment_id=?', $requestData['parent_id']);
				if (!$parentComment || $parentComment['status'] == 2) {
					unset($requestData['parent_id']);
				} else {
					$requestData['parent_uid'] = $parentComment['uid'];
				}
			}

			if (self::model('comment')->insert($requestData, true)) {
				self::model('dynamic')->execute("update wf_dynamic set comment_total=comment_total+1 where dynamic_id={$requestData['type_id']}");

				user_notifyModel::addNotify($dynamicInfo['uid'], notifyModel::TYPE_COMMENT, $requestData['type_id'], $userInfo, $requestData['content']);

				if ($requestData['parent_uid']) { //添加notice信息
					//@todo
				}
				Response::ajax(200, '发表评论成功!');
			}
		} elseif ($requestData['type'] == 3) {
			if (!$dynamicInfo = self::model('activity')->getOne('id=?', $requestData['type_id'])) {
				Response::ajax(NORMAL_ERROR, '参数有误');
			}
			$requestData['title'] = $dynamicInfo['title'];
			if ($requestData['parent_id']) {
				$parentComment = self::model('comment')->getOne('comment_id=?', $requestData['parent_id']);
				if (!$parentComment || $parentComment['status'] == 2) {
					unset($requestData['parent_id']);
				} else {
					$requestData['parent_uid'] = $parentComment['uid'];
				}
			}

			if (self::model('comment')->insert($requestData, true)) {
				self::model('activity')->execute("update wf_activity set comment_total=comment_total+1 where id={$requestData['type_id']}");

				//user_notifyModel::addNotify($dynamicInfo['uid'], notifyModel::TYPE_COMMENT, $requestData['type_id'], $userInfo, $requestData['content']);

				if ($requestData['parent_uid']) { //添加notice信息
					//@todo
				}
				Response::ajax(200, '发表评论成功!');
			}
		} elseif ($requestData['type'] == commentModel::GOODS) {
			if ($requestData['score'] > 5 || $requestData['score'] < 0) {
				Response::ajax(NORMAL_ERROR, '参数错误,商品评价星级有误');
			}
			$orderId = Request::getParams('orderId');
			if (!$orderId || !$orderGoodsInfo = self::model('order_goods')->getOne('order_id=? and uid=? and goods_id=?', [$orderId, $userInfo['uid'], $requestData['type_id']])) {
				Response::ajax(NORMAL_ERROR, '抱歉,您没有未评论的订单商品');
			}
			$requestData['title'] = $orderGoodsInfo['name'];
			if (!$orderInfo = self::model('order')->getOne('id=? and uid=?', [$orderId, $userInfo['uid']])) {
				Response::ajax(NORMAL_ERROR, '抱歉,您没有未评论的订单商品');
			}
			if ($orderInfo['order_status'] != orderModel::ORDER_STATUS_UNCOMMENT) {
				Response::ajax(NORMAL_ERROR, '抱歉,您的订单无法评论');
			}
			if ($orderGoodsInfo['is_comment']) {
				Response::ajax(NORMAL_ERROR, '抱歉,您已评论');
			}
			if (!$commentId = self::model('comment')->insert($requestData, true)) {
				Response::ajax(SYSTEM_ERROR, '添加评论失败,请稍后重试');
			}
			if ($commentList = self::model('order_goods')->getAll('order_id=? and is_comment=?', [$orderId, 0])) {
				if (count($commentList) == 1) {
					self::model('order')->update([
						'order_status' => orderModel::ORDER_STATUS_FINISH,
						'finish_time' => time(),
					], 'id=?', $orderId);
				}
			}
			self::model('goods')->execute("update wf_goods set comment_total=comment_total+1 where goods_id={$requestData['type_id']}");
			self::model('order_goods')->update([
				'comment_id' => $commentId,
				'is_comment' => 1,
			], 'id=?', $orderGoodsInfo['id']);
			Response::ajax(200, '添加评论成功');
		} else {
			Response::ajax(NORMAL_ERROR, '参数有误,添加评论失败');
		}

		Response::ajax(NORMAL_ERROR, '发表评论失败,请稍后再试');
	}
}